import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt

mat = sio.loadmat('ex7data1.mat')
X = mat['X']
print(X.shape)
# (50, 2)

plt.scatter(X[:, 0], X[:, 1])
plt.show()

# 1、X去均值化
X_demean = X - np.mean(X, axis=0)
plt.scatter(X_demean[:, 0], X_demean[:, 1])
plt.show()

# 2、计算协方差矩阵
C = X_demean.T @ X_demean / len(X)
print(C)
# [[1.34852518 0.86535019]
#  [0.86535019 1.02641621]]

# 3、计算特征值，特征向量
U, S, V = np.linalg.svd(C)
print(S)
# [2.06768062 0.30726078]
print(U)
# [[-0.76908153 -0.63915068]
#  [-0.63915068  0.76908153]]
print(V)
# [[-0.76908153 -0.63915068]
#  [-0.63915068  0.76908153]]

U1 = U[:, 0]

# 4、实现降维
X_reduction = X_demean @ U1

plt.figure(figsize=(7, 7))
plt.scatter(X_demean[:, 0], X_demean[:, 1])
plt.plot([0, U1[0]], [0, U1[1]], c='r')
plt.plot([0, U[:, 1][0]], [0, U[:, 1][1]], c='k')
plt.show()


# 5、还原数据
X_restore = X_reduction.reshape(50, 1) @ U1.reshape(1, 2) + np.mean(X, axis=0)
plt.scatter(X[:, 0], X[:, 1])
plt.scatter(X_restore[:, 0], X_restore[:, 1])
plt.show()





